package com.samxcode.leetcode;

/**
 * You are given an n x n 2D matrix representing an image.
 * 
 * Rotate the image by 90 degrees (clockwise).
 * 
 * Follow up: Could you do this in-place? 
 * 
 * Solution: 
 * 把矩阵分为上下左右四部分，然后根据旋转更新矩阵的值
 * 
 * @author Sam
 *
 */
public class RotateImage {

    public void rotate(int[][] matrix) {
        int l = matrix.length;
        if (matrix == null || l == 0) {
            return;
        }
        for (int i = 0; i < l / 2; i++) {
            for (int j = i; j < l - i - 1; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[l - j - 1][i];
                matrix[l - j - 1][i] = matrix[l - i - 1][l - j - 1];
                matrix[l - i - 1][l - j - 1] = matrix[j][l - i - 1];
                matrix[j][l - i - 1] = temp;
            }
        }
    }
}
